package com.cci.kangdao.service.impl;

import com.cci.kangdao.dao.LoginDao;
import com.cci.kangdao.dao.model.RecordLogsT;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.digitalQuery.DigitalQueryTool;
import com.cci.kangdao.redis.RedisClientUtils;
import com.cci.kangdao.service.PropertiesUtils;
import com.cci.kangdao.service.ThirdApiService;
import com.cci.kangdao.utilTool.AESTool;
import com.cci.kangdao.utilTool.ApiErrorCode;
import com.cci.kangdao.utilTool.ParameterTool;
import com.cci.kangdao.utilTool.PublicTool;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
/**
 * @Description: 登录、注册
 * @Company: cty
 * @author 米艳春
 * @date 2018-01-17
 * @version 1.0
 */
@Service("thirdApiService")
public class ThirdApiServiceImpl implements ThirdApiService {
	private Logger log = Logger.getLogger(this.getClass().getName());

	@Resource
	private LoginDao loginDao;
	@Resource
	private PropertiesUtils propertiesUtils;
	@Autowired
	private RedisClientUtils redisClientUtils;
	/**
	 * App数码防伪查询接口
	 * @param map
	 * @return
	 */	
	public ModelAndView digitalQuery(Map map) {
		try {
			HttpServletRequest request = (HttpServletRequest)map.get("request");
			HttpServletResponse response = (HttpServletResponse)map.get("response");
			JSONObject obj = new JSONObject();
			/**
			 * 验证数码内容是否为空
			 */
			if(map.get("digitalCode") == null  || map.get("digitalCode").toString().equals("")){
				obj.put("status", -5);
				obj.put("msg", ApiErrorCode.getInstance().getErrorMsg(-5));
				
				//返回客户
				ParameterTool.writeResponse(response, obj.toString());	
				obj = null;
				return null;
			}
			
			String digitalCode = map.get("digitalCode").toString();
			/**
			 * DigitalQueryTool查询数码真伪
			 */
			List<Object> list = DigitalQueryTool.searchDigital(digitalCode, "127.0.0.1");
			/**
			 * systemState：服务执行状态
			 *	001 数码为真（首次验证）
			 *	002 数码为真但被查询过（重复查询）
				003数码不存在
				004 数码长度不对/获取二维码解析错误
				005 查询异常
				006 帐号或密码错误
				007 数码必须是数值
				008 参数为空或值错误
				009 接口授权时间不在许可范围之内
				101 IP访问限制
				102 查询异常网络繁忙
				113 查询的IP超过最大的次数限制
				115 签名错误
			 */
			String code = ""+list.get(2);
			if(code.equals("001") || code.equals("002")){
				obj.put("status", 0);
				obj.put("msg", "");
				obj.put("content", list.get(1));
			}else{
				obj.put("status", -8);
				obj.put("msg", ApiErrorCode.getInstance().getErrorMsg(-8));
				obj.put("content", "");
			}
//			String chaxune = "<?xml version='1.0' encoding='utf-8' ?><messageinfo><item accode='1641513944784617'><reply>您所查询的防伪码已在2015年09月28日08时26分查询过，此次为第77次查询，请核对防伪码颜色从左至右各位数字的颜色是否依次为：</br><table><tr><td align=\"center\"><font color=\"#221E1F\">1</font></td><td align=\"center\"><font color=\"#EC008C\">6</font></td><td align=\"center\"><font color=\"#00A650\">4</font></td><td align=\"center\"><font color=\"#EC008C\">1</font></td><td align=\"center\"><font color=\"#221E1F\">5</font></td><td align=\"center\"><font color=\"#00A650\">1</font></td><td align=\"center\"><font color=\"#221E1F\">3</font></td><td align=\"center\"><font color=\"#00AEEF\">9</font></td><td align=\"center\"><font color=\"#00A650\">4</font></td><td align=\"center\"><font color=\"#00AEEF\">4</font></td><td align=\"center\"><font color=\"#00A650\">7</font></td><td align=\"center\"><font color=\"#EC008C\">8</font></td><td align=\"center\"><font color=\"#221E1F\">4</font></td><td align=\"center\"><font color=\"#221E1F\">6</font></td><td align=\"center\"><font color=\"#00AEEF\">1</font></td><td align=\"center\"><font color=\"#00AEEF\">7</font></td></tr><tr align=\"center\"><td align=\"center\"><font color=\"#221E1F\">黑色</font></td><td align=\"center\"><font color=\"#EC008C\">红色</font></td><td align=\"center\"><font color=\"#00A650\">绿色</font></td><td align=\"center\"><font color=\"#EC008C\">红色</font></td><td align=\"center\"><font color=\"#221E1F\">黑色</font></td><td align=\"center\"><font color=\"#00A650\">绿色</font></td><td align=\"center\"><font color=\"#221E1F\">黑色</font></td><td align=\"center\"><font color=\"#00AEEF\">蓝色</font></td><td align=\"center\"><font color=\"#00A650\">绿色</font></td><td align=\"center\"><font color=\"#00AEEF\">蓝色</font></td><td align=\"center\"><font color=\"#00A650\">绿色</font></td><td align=\"center\"><font color=\"#EC008C\">红色</font></td><td align=\"center\"><font color=\"#221E1F\">黑色</font></td><td align=\"center\"><font color=\"#221E1F\">黑色</font></td><td align=\"center\"><font color=\"#00AEEF\">蓝色</font></td><td align=\"center\"><font color=\"#00AEEF\">蓝色</font></td></tr></table>，若颜色符合且查询时间与您首次查询时间相符，则该零件为康明斯公司生产的正宗产品，否则请谨防假冒。并请拨打咨询热线 021-26095599。谢谢您的使用！</reply></item></messageinfo>";
//			obj.put("status", 0);
//			obj.put("msg", "");
//			obj.put("content", chaxune);
						
			//返回客户
			ParameterTool.writeResponse(response, obj.toString());	
			obj = null;			
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return null;
	}
	
	
	/**
	 * App上传物流跟踪接口
	 * @param map
	 * @return
	 */
	public ModelAndView uploadLogistics(Map map){
		try {
			HttpServletRequest request = (HttpServletRequest)map.get("request");
			HttpServletResponse response = (HttpServletResponse)map.get("response");
			JSONObject obj = new JSONObject();
			
			String type = "";
			if(map.get("type") != null){
				type = map.get("type").toString();
				type = AESTool.decryptString(type);
			}
			String mobile = "";
			if(map.get("mobile") != null){
				mobile = map.get("mobile").toString();
				mobile = AESTool.decryptString(mobile);
			}
			/**
			 * 获取用户
			 */
			String userid = request.getHeader("userid");
			userid = AESTool.decryptString(userid);
			String token = request.getHeader("Authorization");
			UserT userT = (UserT)redisClientUtils.getObject("userT"+userid);
			
			/**
			 * 报错日志
			 */
			RecordLogsT recordLogs = new RecordLogsT();
			recordLogs.setUserId(userT.getId());
			recordLogs.setLinkUrl("uploadLogistics.aspx");
			recordLogs.setActionName("type="+type+"&mobile="+mobile);
			recordLogs.setResponseName("");
			recordLogs.setIp(PublicTool.getIp(request));
			recordLogs.setResidenceTime(0.0d);
			
			loginDao.insertRecordLogs(recordLogs);
			
			obj.put("status", 0);
			obj.put("msg", "");
						
			//返回客户
			ParameterTool.writeResponse(response, obj.toString());	
			obj = null;			
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return null;
	}
}
